﻿using System.Collections.Generic;
using Microsoft.Xna.Framework.Content;
using Imaginecup.Recycling.GameMan;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Imaginecup.recycling.Object;
using Imaginecup.recycling.Stage;
using Imaginecup.shareData;
using Imaginecup.util;
using Imaginecup.recycling.Factory;
using Imaginecup.gameState.recycling.Entity;
using Imaginecup.util.resourceMan;
using Imaginecup.util.StaticValue;
using Imaginecup.factory;

namespace Imaginecup.gameState.recycling
{
    public enum Button_State
    {
        NONE,
        OVER,
        PRESSED,
        LOCK
    }

    public class SelectStage : BaseStage, IStage, IStageBaseBehavior
    {
        private ContentManager m_Content;
        private List<SelectStageItemInfo> m_StageSelectList;

        private int m_MaxStageRow = 2;
        private int m_MaxStageCol = 5;
        private DrawNumber m_DrawNumber;
        private Rectangle m_ExitRect;

        public SelectStage()
        {
            m_StageSelectList = new List<SelectStageItemInfo>(StaticValues.MAX_STAGE);
            m_Content = new ContentManager(StateShareData.GetShareData().GetGame().Services);
            m_Content.RootDirectory = "Content/ui/";
            Initialize();
        }

        protected override void Initialize()
        {
            ResourceMan.GetResourceMan().SetTextureFrame("Numbers", m_Content.Load<Texture2D>("sel_num"));
            m_DrawNumber = new DrawNumber("Numbers");
            ResourceManager manager = ResourceManager.GetInstance();
            manager.SetTexture("StageSelectSheet", m_Content.Load<Texture2D>("sel_butt"));
            manager.SetTexture("StageSelect_Front", m_Content.Load<Texture2D>("sel_2"));
            manager.SetTexture("StageSelect_Background", m_Content.Load<Texture2D>("sel_1"));
            manager.SetTexture("StageSelect_Exit", m_Content.Load<Texture2D>("sel_exit"));

            m_ExitRect = new Rectangle(724, 26, manager.GetContent("StageSelect_Exit").Width, manager.GetContent("StageSelect_Exit").Height);

            // 현재 열린 스테이지의 수
            //SelectStageItemInfo.OpenStageCount = 0;

            // 스테이지의 정보를 저장 할 리스트 초기화
            int count = 0;
            int textureWidth = manager.GetContent("StageSelectSheet").Width / 4;
            int textureHeight = manager.GetContent("StageSelectSheet").Height;
            for (int row = 0; row < m_MaxStageRow; row++)
                for (int col = 0; col < m_MaxStageCol; col++)
                {
                    SelectStageItemInfo selectDrawInfo = new SelectStageItemInfo();
                    selectDrawInfo.MyNumber = count + 1;
                    if (SelectStageItemInfo.OpenStageCount >= count++)
                        selectDrawInfo.CurrentState = Button_State.NONE;
                    else
                        selectDrawInfo.CurrentState = Button_State.LOCK;

                    selectDrawInfo.CollisionRect = new Rectangle(112 + (col * 118), 133 + (row * 119), textureWidth, textureHeight);

                    m_StageSelectList.Add(selectDrawInfo);
                }

            // 이미지 사전 셋팅
            SelectStageItemInfo.SourceDic.Clear();
            SelectStageItemInfo.SourceDic.Add(Button_State.NONE, new Rectangle(textureWidth * 0, 0, textureWidth, textureHeight));
            SelectStageItemInfo.SourceDic.Add(Button_State.OVER, new Rectangle(textureWidth * 1, 0, textureWidth, textureHeight));
            SelectStageItemInfo.SourceDic.Add(Button_State.PRESSED, new Rectangle(textureWidth * 2, 0, textureWidth, textureHeight));
            SelectStageItemInfo.SourceDic.Add(Button_State.LOCK, new Rectangle(textureWidth * 3, 0, textureWidth, textureHeight));
        }

        public override void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(
                ResourceManager.GetInstance().GetContent("StageSelect_Background"),
                Vector2.Zero,
                Color.White
                );
            spriteBatch.Draw(
                ResourceManager.GetInstance().GetContent("StageSelect_Front"),
                Vector2.Zero,
                Color.White
                );
            spriteBatch.Draw(
                ResourceManager.GetInstance().GetContent("StageSelect_Exit"),
                new Vector2(m_ExitRect.X, m_ExitRect.Y),
                Color.White
                );
            spriteBatch.End();

            foreach (SelectStageItemInfo selectState in m_StageSelectList)
            {
                spriteBatch.Begin();
                spriteBatch.Draw(
                    ResourceManager.GetInstance().GetContent("StageSelectSheet"),
                    new Vector2(selectState.CollisionRect.X, selectState.CollisionRect.Y),
                    SelectStageItemInfo.SourceDic[selectState.CurrentState],
                    Color.White
                    );
                spriteBatch.End();

                if (selectState.CurrentState != Button_State.LOCK)
                {
                    int x = (int)selectState.CenterPosition.X - (ResourceMan.GetResourceMan().GetTexture("Numbers").Width / 10) * (selectState.MyNumber/10 + 1) / 2;
                    int y = (int)selectState.CenterPosition.Y - ResourceMan.GetResourceMan().GetTexture("Numbers").Height / 2;

                    m_DrawNumber.Draw(spriteBatch, x, y, selectState.MyNumber.ToString());
                }
            }
            
        }

        public override void Update(GameTime gameTime)
        {
            StageMouseProcess();
            BackBtnProcess();
        }

        // X버튼 처리
        private void BackBtnProcess()
        {
            IMouseInputHandler input = MouseInputHandler.GetInputHandler();
            if (m_ExitRect.Contains((int)input.Position.X, (int)input.Position.Y))
            {
                if(input.IsOneClickUp())
                {
                    StateShareData.GetShareData().GetGameStateMan().PopState();
                    StateShareData.GetShareData().GetGameStateMan().PushState((IGameState)StateFactory.GetStateFactory().GetObject(StateFactoryID.State_ChoiceMap));
                }
            }
        }

        //마우스 처리
        private void StageMouseProcess()
        {
            IMouseInputHandler input = MouseInputHandler.GetInputHandler();
            int count = 0;
            foreach (SelectStageItemInfo selectStageItem in m_StageSelectList)
            {
                if (SelectStageItemInfo.OpenStageCount >= count++)
                {
                    if (selectStageItem.CollisionRect.Contains((int)input.Position.X, (int)input.Position.Y))
                    {
                        if (input.IsOneClick())
                        {
                            selectStageItem.CurrentState = Button_State.PRESSED;
                        }
                        else if (input.IsOneClickUp())
                        {
                            m_StageManager.ChangeStage(StageID.STAGE1);
                            return;
                        }
                    }
                    else
                    {
                        selectStageItem.CurrentState = Button_State.NONE;
                    }
                }
            }
        }

        public void Release()
        {
            // 매니저 해제
            //m_GameManager.RemoveAll();
            // 사용한 리소스 해제
            ResourceMan.GetResourceMan().Release("Numbers");
            ResourceManager.GetInstance().DeleteTexture("StageSelectSheet");
            ResourceManager.GetInstance().DeleteTexture("StageSelect_Front");
            ResourceManager.GetInstance().DeleteTexture("StageSelect_Background");
            ResourceManager.GetInstance().DeleteTexture("StageSelect_Exit");
            m_Content.Unload();
            m_StageSelectList.Clear();
        }
    }
}
